package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {


    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetMealDishMapper setMealDishMapper;

    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        //向dish表添加数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        //获取flavor中的dishId
        Long id = dish.getId();

        //向flavor表添加数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            dishFlavorMapper.insert(flavors);
        }
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> dishVOS = dishMapper.page(dishPageQueryDTO);
        return Result.success(new PageResult(dishVOS.getTotal(), dishVOS.getResult()));

    }

    @Transactional
    @Override
    public Result<String> delete(List<Long> ids) {
        //是否停用
        ids.forEach(id -> {
            if (dishMapper.getById(id).getStatus() == StatusConstant.ENABLE)
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        });

        //是否关联套餐
        List<Long> setmealIds = setMealDishMapper.getSetmealIdsByDishIds(ids);
        if (setmealIds != null)
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);


        //删除菜品数据 和 口味数据
//        ids.forEach(id->{
//            dishMapper.deleteById(id);
//            dishFlavorMapper.deleteByDishId(id);
//        });

        dishMapper.deleteIds(ids);
        dishFlavorMapper.deleteByDishIds(ids);


        return null;
    }

    @Override
    public Result<String> changeStatus(int status, Long id) {
        dishMapper.updateStatus(status, id);
        return Result.success();

    }

    @Override
    public Result<DishVO> getById(Long id) {
        DishVO dishVO = new DishVO();
        Dish dish = dishMapper.getById(id);
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(flavors);
        return Result.success(dishVO);
    }

    @Override
    public Result<String> updateDishWithFlavors(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        if (dishFlavors != null && dishFlavors.size()>0) {
            dishFlavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
            dishFlavorMapper.deleteByDishId(dish.getId());
            dishFlavorMapper.insert(dishFlavors);


        }
        return Result.success();

    }

    @Override
    public Result<List<Dish>> getByCategory(Long categoryId) {
        List<Dish> dishes=dishMapper.getByCategoryId(categoryId);
        return Result.success(dishes);
    }
}
